﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTH_BUSINESS;
using QLTH_DTO;
using unitlControl;
namespace QuanLy_THPT_S4
{
    public partial class frmDSLop : Form
    {
        private readonly clsLopBU lopBU;
        private readonly clsLopDTO lopDTO;
        private readonly clsNamHocBU nhBU;
        private readonly clsKhoiBU kBU;
        private readonly clsBanHocBU bBU;
        public frmDSLop()
        {
            InitializeComponent();
            lopBU = new clsLopBU();
            lopDTO = new clsLopDTO();
            nhBU = new clsNamHocBU();
            kBU = new clsKhoiBU();
            bBU = new clsBanHocBU();
        }

        private string malop(String _manamhoc)
        {
            if (lopBU.macuoi() == "" || lopBU.macuoi() == null)
            {
                return toancuc.Tao_ma(null, "LH" + _manamhoc.Substring(2, 4), 2);
            }
            else if (_manamhoc.Substring(2, 4) == lopBU.macuoi().Substring(2, 4))
            {
                return toancuc.Tao_ma(lopBU.macuoi(), "LH" + lopBU.macuoi().Substring(2, 4), 2);
            }
            else
            {
                return toancuc.Tao_ma(null, "LH" + _manamhoc.Substring(2, 4), 2);
            }
           
          
         }
        private void frmDSLop_Load(object sender, EventArgs e)
        {
            ComboUtils.SetDataSuorce(cmbNH,nhBU.DanhSachNamHoc(),"MANH","TENNAMHOC",0);
            ComboUtils.SetDataSuorce(cmbKhoi, kBU.DanhSachKhoi(), "MAKHOI", "TENKHOI", 0);
            ComboUtils.SetDataSuorce(cmbBAN, bBU.DanhSachBan(), "MABAN", "TENBAN", 0);
            txtNamHocHT.Text = nhBU.manhtHientai().Rows[0]["TENNAMHOC"].ToString();
            spinEdit1.Properties.MinValue = 0;
            spinEdit1.Properties.MaxValue = 30;
            gridControl1.DataSource = lopBU.DanhSachLop();
           
          
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            lopDTO.Namhoc.Manh = nhBU.manhtHientai().Rows[0]["MANH"].ToString();
            lopDTO.Khoi.Makhoi = ComboUtils.LayGT_Combobox(cmbKhoi);
            lopDTO.Banhoc.Maban = ComboUtils.LayGT_Combobox(cmbBAN);
            for (int i = 1; i <= int.Parse(spinEdit1.Value.ToString()); i++)
            {
                lopDTO.Malop = malop(lopDTO.Namhoc.Manh);
                lopDTO.Tenlop = ComboUtils.LayGTHienthi(cmbKhoi).Substring(5, 2)+"A"+i;
                lopBU.Them_lop(lopDTO);
            }
            frmDSLop_Load(sender, e);
 
    
        }
    }
}
